import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

ScrollView{
    clip: true
    ColumnLayout {
        Rectangle{
            Layout.fillWidth: true
            width: parent.width
            height: 70
            color: "#00000000"
            Text {
                x:10
                text: qsTr("推荐内容")
                font.family: window.mFONT_FAMILY
                font.pointSize: 25
                color:"#eeffffff"
            }
        }


        MusicBannerView{
            id:bannerView
            Layout.preferredWidth: window.width-200
            Layout.preferredHeight: (window.width-200)*0.3
            Layout.fillHeight: true
            Layout.fillWidth: true

        }
        Rectangle{
            Layout.fillWidth: true
            width: parent.width
            height: 70
            color: "#00000000"
            Text {
                x:10
                text: qsTr("热门歌单")
                font.family: window.mFONT_FAMILY
                font.pointSize: 25
                color:"#eeffffff"
            }
        }
        MusicGridHotView{
            id:hotView
            Layout.fillWidth: true
            Layout.fillHeight: true
            Layout.preferredHeight: (window.width-250)*0.2*4+30*4+20
        }
        Rectangle{
            Layout.fillWidth: true
            width: parent.width
            height: 70
            color: "#00000000"
            Text {
                x:10
                text: qsTr("新歌推荐")
                font.family: window.mFONT_FAMILY
                font.pointSize: 25
                color:"#eeffffff"
            }
        }

        MusicGridLatestView{
            id:latestView
            Layout.fillWidth: true
            Layout.fillHeight: true
            Layout.preferredHeight: (window.width-230)*0.1*10+20
            Layout.bottomMargin: 20
        }

        Component.onCompleted: {
            getBannerList()
        }

        function getBannerList(){
            loading.open()
            function onReply(reply){
                loading.close()
                http.onReplySignal.disconnect(onReply)
                try{
                    if(reply.length<1)
                    {
                        notification.openError("请求轮播图为空！")

                        return
                    }

                    var banners = JSON.parse(reply).banners
                    //console.log("Banner list:", banners)
                    bannerView.bannerList = banners
                    getHotList()
                }catch(err){
                    notification.openError("请求轮播图出错！")
                }
            }
            http.onReplySignal.connect(onReply)
            http.connet("banner")
        }
        function getHotList(){
            loading.open()
            function onReply(reply){
                loading.close()
                http.onReplySignal.disconnect(onReply)
                try{
                    if(reply.length<1)
                    {
                        notification.openError("请求热门图片为空！")
                        return
                    }
                    var playlists = JSON.parse(reply).playlists
                    //console.log("Banner list:", banners)
                    hotView.list = playlists
                    getLatestList()
                }catch(err){
                    notification.openError("请求热门图片出错！")
                }
            }
            http.onReplySignal.connect(onReply)
            http.connet("top/playlist/highquality?limit=20")
        }
        function getLatestList(){
            loading.open()
            function onReply(reply){
                http.onReplySignal.disconnect(onReply)
                loading.close()
                try{
                    if(reply.length<1)
                    {
                        notification.openError("请求热门推荐为空！")
                        return
                    }
                    var latestList = JSON.parse(reply).data
                    //console.log("Banner list:", latestList)
                    latestView.list =latestList.slice(0,30)
                }catch(err){
                    notification.openError("请求热门推荐出错！")
                }
            }

            http.onReplySignal.connect(onReply)
            http.connet("top/song")
        }
    }
}

